package com.dk.jwzx.reptile;

import com.dk.util.StreamUtil;

import java.net.HttpURLConnection;
import java.net.URL;

public abstract class ReptileBase implements Reptile,ReptileConfig{
    private String requestMethod="GET";
    private int timeout=1000;
    private HttpURLConnection connection;

    @Override
    public void init() {
        this.requestMethod=REQUEST_METHOD;
        this.timeout=TIMEOUT;
    }

    @Override
    public <T> T crawling(String url) {
        try{
            connection=(HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod(requestMethod);
            connection.setConnectTimeout(timeout);
            connection.connect();
            return doCrawling(StreamUtil.getStringFromStream(connection.getInputStream()));
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            connection.disconnect();
        }
    }

    public abstract <T> T doCrawling(String s);
}
